<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 400px;
            height: 300px;
            border: 1px solid black;
            margin: 100px auto;
        }

        ul {
            list-style: none;
            display: flex;
            height: 40px;
            line-height: 40px;
            text-align: center;
            border-bottom: solid 1px black
        }

        li {
            /* width: 100px; */
            flex: 1;
            cursor: pointer;
        }

        .box li:active {
            background: #99f
        }

        .box li.active {
            background: red
        }

        .cont div {
            height: 259px;
            display: none;
        }

        .cont .cont1 {
            background: #66f;
            display: block
        }

        .cont .cont2 {
            background: #6f6;
        }

        .cont .cont3 {
            background: #f66;
        }

        .cont .cont4 {
            background: #ff6;
        }
    </style>
</head>

<body>
    <div class="box">
        <ul>
            <li class="active">张学友</li>
            <li>刘德华</li>
            <li>郭富城</li>
            <li>黎明</li>
        </ul>
        <div class="cont">
            <div class="cont1">
                歌神(张学友)
            </div>
            <div class="cont2">
                影帝(刘德华)
            </div>
            <div class="cont3">
                舞王(郭富城)
            </div>
            <div class="cont4">
                人气王(黎明)
            </div>
        </div>
    </div>
</body>
<script>
        function Tab(){
            // 写属性，变量
            // 选择元素
            this.li = document.querySelectorAll(".box li");
            this.div = document.querySelectorAll(".cont>div");
            this.index = 0;
            this.addEvent();
        }
        
        Tab.prototype.addEvent=function(){
            const that=this;
            for(let i=0;i<this.li.length;i++){
                this.li[i].xuhao=i
                this.li[i].onclick = function(){
                    that.hide();
                    that.getIndex(this);
                }
            }
        }

        Tab.prototype.hide=function(){
            this.li[this.index].className="";
            this.div[this.index].style.display="none"
        }

        Tab.prototype.getIndex=function(now){
            this.index=now.xuhao;
            this.show()
        }

        Tab.prototype.show=function(){
            this.li[this.index].className="active";
            this.div[this.index].style.display="block"
        }
        new Tab();


</script>

</html>